package com.erb.service.zwj.impl;

import com.erb.constants.AdminInformConstants;
import com.erb.entity.Inform;
import com.erb.entity.InformDetailVo;
import com.erb.entity.InformVo;
import com.erb.mapper.InformMapper;
import com.erb.service.zwj.IInformService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

@Transactional
@Service("informService")
public class InformServiceImpl implements IInformService {
	@Resource
	private InformMapper informMapper;

	@Override
	public List<Inform> findInforms() {
		return informMapper.findInforms();
	}

	@Override
	public List<InformVo> findInformsByRecipientsId(int recipientsId) {
		return informMapper.findInformsByRecipientId(recipientsId);
	}

	/**
	 * 查询前五条未读通知
	 *
	 * @param recipientsId 接收者ID
	 * @return
	 */
	@Override
	public List<InformVo> findInformVosForUnreadTop5(int recipientsId) {
		return informMapper.findInformVosForUnreadTop5(recipientsId);
	}

	/**
	 * 查询未读通知总数
	 *
	 * @param recipientsId
	 * @return
	 */
	@Override
	public int findCountUnread(int recipientsId) {
		return informMapper.findCountUnread(recipientsId);
	}

	/**
	 * 查看通知详细
	 *
	 * @param informId
	 * @return
	 */
	@Override
	public InformDetailVo findInformDetail(int informId) {
		if (informId <= 0) {
			return null;
		}
		return informMapper.findInformById(informId);
	}

	/**
	 * 读取通知，并获得通知详细
	 *
	 * @param informId
	 * @return
	 */
	@Override
	public InformDetailVo readInformDetail(int informId) {
		if (informId <= 0) {
			return null;
		}
		this.informMapper.updateReadedOfTrue(informId);
		return this.findInformDetail(informId);
	}

	/**
	 * 保存通知
	 *
	 * @param inform
	 * @return
	 */
	@Override
	public boolean saveInform(Inform inform) {
		if (ObjectUtils.isEmpty(inform)) {
			return false;
		}

		inform.setReaded(AdminInformConstants.INFORM_DEFAULT_READED);
		return informMapper.insertInform(inform) > 0;
	}

	@Override
	public List<InformVo> findInformsByRecipientsIdForLoan(int recipientsId) {
		return informMapper.findInformsByRecipientsIdForLoan(recipientsId);
	}
}
